package com.smart.campus.web.controller.student;

import cn.hutool.core.date.DateUtil;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.school.domain.ExamAnswer;
import com.smart.campus.school.domain.ExamQuestion;
import com.smart.campus.school.domain.WrongQuestion;
import com.smart.campus.school.service.IExamAnswerService;
import com.smart.campus.school.service.IExamQuestionService;
import com.smart.campus.school.service.IStudentPaperService;
import com.smart.campus.school.service.IWrongQuestionService;
import com.smart.campus.web.utils.StudentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 学生信息Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-18
 */
@Controller
@RequestMapping("/student/question")
public class StudentQuestionController extends BaseController
{
    private String prefix = "student";

    @Autowired
    private IWrongQuestionService wrongQuestionService;
    @Autowired
    private IExamQuestionService examQuestionService;
    @Autowired
    private IExamAnswerService examAnswerService;

    @Autowired
    private IStudentPaperService studentPaperService;
    @GetMapping("/wrong")
    public String wrongQuestion()
    {
        return prefix + "/question/wrongQuestion";
    }

    /**
     * 修改试题
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamQuestion examQuestion = examQuestionService.selectExamQuestionById(id);
        mmap.put("examQuestion", examQuestion);
        return prefix + "/question/detail";
    }

    /**
     * 查询错题记录列表
     */
    @PostMapping("/wrong/list")
    @ResponseBody
    public TableDataInfo list(WrongQuestion wrongQuestion)
    {
        startPage();
        wrongQuestion.setStudentId(StudentUtil.getStudentId());
        wrongQuestion.setStatus(1);
        List<WrongQuestion> list = wrongQuestionService.selectWrongQuestionList(wrongQuestion);
        return getDataTable(list);
    }

    /**
     * 掌握错题
     */
    @PostMapping("/wrong/grasp")
    @ResponseBody
    public AjaxResult grasp(Long id)
    {
        WrongQuestion wrongQuestion = new WrongQuestion();
        wrongQuestion.setId(id);
        wrongQuestion.setStatus(0);
        wrongQuestionService.updateWrongQuestion(wrongQuestion);
        return AjaxResult.success();
    }

    /**
     * 打印练习卷
     */
    @GetMapping("/printQuestion")
    public String printQuestion(String questionIds,ModelMap mmap) {

        Map<Integer,List<ExamQuestion>> map = new TreeMap<>();
        if(StringUtils.isNotEmpty(questionIds)){
            String[] questionIdArray = questionIds.split(",");
            for (String questionId : questionIdArray) {
                ExamQuestion examQuestion = examQuestionService.selectExamQuestionById(Long.valueOf(questionId));
                //按题型分组
                if(map.containsKey(examQuestion.getType())){
                    map.get(examQuestion.getType()).add(examQuestion);
                }else {
                    List<ExamQuestion> result = new ArrayList<>();
                    result.add(examQuestion);
                    map.put(examQuestion.getType(),result);
                }

            }
        }
        mmap.put("map", map);
        mmap.put("date", DateUtil.thisYear()+"年第"+DateUtil.weekOfYear(new Date())+"周");
        return prefix + "/question/print";
    }
    /**
     * 打印练习卷答案
     */
    @GetMapping("/printAnswer")
    public String printAnswer(String questionIds,ModelMap mmap) {

        Map<Integer,Map<Long, List<ExamAnswer>>> typeMap = new TreeMap<>();
        if(StringUtils.isNotEmpty(questionIds)){
            String[] questionIdArray = questionIds.split(",");
            for (String questionId : questionIdArray) {
                ExamQuestion question = examQuestionService.selectExamQuestionById(Long.valueOf(questionId));
                ExamAnswer examAnswer = new ExamAnswer();
                examAnswer.setQuestionId(Long.valueOf(question.getId()));
                List<ExamAnswer> answerList = examAnswerService.selectExamAnswerList(examAnswer);
                //按试题类型分组
                if(typeMap.containsKey(question.getType())){
                    Map<Long, List<ExamAnswer>> resMap = typeMap.get(question.getType());
                    resMap.put(Long.valueOf(question.getId()),answerList);
                }else {
                    Map<Long, List<ExamAnswer>> resMap = new LinkedHashMap<>();
                    resMap.put(Long.valueOf(question.getId()),answerList);
                    typeMap.put(question.getType(),resMap);
                }

            }
        }
        mmap.put("map", typeMap);
        mmap.put("date", DateUtil.thisYear()+"年第"+DateUtil.weekOfYear(new Date())+"周");
        return prefix + "/question/printAnswer";
    }
}
